const mongoose = require("mongoose");
const { Schema } = mongoose;
const bcrypt = require("bcrypt");
const UserSchema = new Schema(
    {
        avatar: String,
        username: { type: String, required: true },
        email: { type: String, required: true },
        password: {
            type: String,
            set: val => {
                return bcrypt.hashSync(val, 10); //加密等级一般10-12
            },
            get: val => {
                return val;
            },
        },
        role: { type: String, required: true },
    },
    { timestamps: { createdAt: "createTime", updatedAt: "updateTime" } }
);

module.exports = mongoose.models.User || mongoose.model("User", UserSchema);
